﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;

using Survey.Common;
using Survey.Common.Validation;
using Survey.Common.Data.Services;
using Survey.Entity.Services;

namespace Survey.Business.Services
{
    public class UserFilterValidator : FilterValidatorBase<UserFilter, int>
    {
        protected override void Init(RuleCollection<UserFilter> rules) { base.Init(rules); }
    }
    public class UserValidator : EntityValidatorBase<User, int, IUserDao>
    {
        protected override void Init(RuleCollection<User> rules)
        {
            // string
            rules.For(entity => entity.FirstName).RequireLengthBetween(1, 150);
            rules.For(entity => entity.LastName).RequireLengthBetween(1, 150);

            // unique, user typed ID
            rules.For(entity => entity.Login).RequireLengthBetween(2, 150)
                .BreakOnError();
            rules.IsUnique(entity => Dao.Find(entity.ID, new UserFilter { LoginExact = entity.Login }))
                .BreakOnError();
        }
        public override string ValidationRulesToString(string validationType, string prefix)
        {
            return base.ValidationRulesToString(validationType, prefix)
                   + new MemberValidator().ValidationRulesToString(validationType, prefix.Extend("Member"))
                   ;
        }
    }

    public class MemberValidator : ValidatorBase<Member>
    {
        protected override void Init(RuleCollection<Member> rules)
        {
            rules.For(entity => entity.UserName).RequireLengthBetween(3, 150).BreakOnError();
            rules.For(entity => entity.FirstName).RequireLengthBetween(1, 150);
            rules.For(entity => entity.Surname).RequireLengthBetween(1, 150);

            // regular
            rules.MatchEmail(entity => entity.Email);

            // Add
            rules.For(entity => entity.Email).RequireLengthBetween(7, 150).BreakOnError()
                .WhenCreating();
            rules.For(entity => entity.Password).RequireLengthBetween(8, 50).BreakOnError()
                .WhenCreating();
            rules.For(entity => entity.ConfirmedPassword).RequireLengthBetween(8, 50).BreakOnError()
                .WhenCreating();
            rules.Assure(NewPasswordsAreEqual).WhenCreating();

            // Update
            rules.For(entity => entity.NewPassword).RequireLengthBetween(8, 50).BreakOnError()
                .WhenUpdating();
            rules.For(entity => entity.ConfirmedPassword).RequireLengthBetween(8, 50).BreakOnError()
                .WhenUpdating();
            rules.Assure(UpdatedPasswordsAreEqual).WhenUpdating();
        }

        #region Check
        protected virtual ValidationResult NewPasswordsAreEqual(Member entity, out ValidationMessage message)
        {
            message = null;
            var areEqual = entity.Password.IsNotEmpty()
                && entity.ConfirmedPassword.IsNotEmpty()
                && entity.Password.Equals(entity.ConfirmedPassword, StringComparison.Ordinal);
            if (areEqual)
            {

                return ValidationResult.IsValid;
            }
            message = new ValidationMessage("Passwords are not the same");
            return ValidationResult.Error;
        }
        protected virtual ValidationResult UpdatedPasswordsAreEqual(Member entity, out ValidationMessage message)
        {
            message = null;
            var areEqual = entity.NewPassword.IsNotEmpty()
                && entity.ConfirmedPassword.IsNotEmpty()
                && entity.NewPassword.Equals(entity.ConfirmedPassword, StringComparison.Ordinal);
            if (areEqual)
            {

                return ValidationResult.IsValid;
            }
            message = new ValidationMessage("Passwords are not the same");
            return ValidationResult.Error;
        }
        #endregion Check
    }
}
